{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Hours of Training to Win a Competition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minNumberOfHours"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #赢得比赛需要的最少训练时长"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>你正在参加一场比赛，给你两个 <strong>正</strong> 整数 <code>initialEnergy</code> 和 <code>initialExperience</code> 分别表示你的初始精力和初始经验。</p>\n",
    "\n",
    "<p>另给你两个下标从 <strong>0</strong> 开始的整数数组 <code>energy</code> 和 <code>experience</code>，长度均为 <code>n</code> 。</p>\n",
    "\n",
    "<p>你将会 <strong>依次</strong> 对上 <code>n</code> 个对手。第 <code>i</code> 个对手的精力和经验分别用 <code>energy[i]</code> 和 <code>experience[i]</code> 表示。当你对上对手时，需要在经验和精力上都 <strong>严格</strong> 超过对手才能击败他们，然后在可能的情况下继续对上下一个对手。</p>\n",
    "\n",
    "<p>击败第 <code>i</code> 个对手会使你的经验 <strong>增加</strong> <code>experience[i]</code>，但会将你的精力 <strong>减少</strong>&nbsp; <code>energy[i]</code> 。</p>\n",
    "\n",
    "<p>在开始比赛前，你可以训练几个小时。每训练一个小时，你可以选择将增加经验增加 1 <strong>或者</strong> 将精力增加 1 。</p>\n",
    "\n",
    "<p>返回击败全部 <code>n</code> 个对手需要训练的 <strong>最少</strong> 小时数目。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]\n",
    "<strong>输出：</strong>8\n",
    "<strong>解释：</strong>在 6 小时训练后，你可以将精力提高到 11 ，并且再训练 2 个小时将经验提高到 5 。\n",
    "按以下顺序与对手比赛：\n",
    "- 你的精力与经验都超过第 0 个对手，所以获胜。\n",
    "  精力变为：11 - 1 = 10 ，经验变为：5 + 2 = 7 。\n",
    "- 你的精力与经验都超过第 1 个对手，所以获胜。\n",
    "  精力变为：10 - 4 = 6 ，经验变为：7 + 6 = 13 。\n",
    "- 你的精力与经验都超过第 2 个对手，所以获胜。\n",
    "  精力变为：6 - 3 = 3 ，经验变为：13 + 3 = 16 。\n",
    "- 你的精力与经验都超过第 3 个对手，所以获胜。\n",
    "  精力变为：3 - 2 = 1 ，经验变为：16 + 1 = 17 。\n",
    "在比赛前进行了 8 小时训练，所以返回 8 。\n",
    "可以证明不存在更小的答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>你不需要额外的精力和经验就可以赢得比赛，所以返回 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == energy.length == experience.length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= initialEnergy, initialExperience, energy[i], experience[i] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-hours-of-training-to-win-a-competition](https://leetcode.cn/problems/minimum-hours-of-training-to-win-a-competition/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-hours-of-training-to-win-a-competition](https://leetcode.cn/problems/minimum-hours-of-training-to-win-a-competition/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5\\n3\\n[1,4,3,2]\\n[2,6,3,1]', '2\\n4\\n[1]\\n[3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def minNumberOfHours(self, eng: int, exp: int, energy: List[int], experience: List[int]) -> int:\n",
    "\n",
    "        ans = 0\n",
    "\n",
    "        for x, y in zip(energy, experience):\n",
    "\n",
    "            if eng <= x:\n",
    "\n",
    "                ans += x + 1 - eng\n",
    "\n",
    "                eng = x + 1  # 补到刚好超过\n",
    "\n",
    "            eng -= x\n",
    "\n",
    "            if exp <= y:\n",
    "\n",
    "                ans += y + 1 - exp\n",
    "\n",
    "                exp = y + 1  # 补到刚好超过\n",
    "\n",
    "            exp += y\n",
    "\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: list[int], experience: list[int]) -> int:\n",
    "        ans = 0\n",
    "        if initialEnergy <= sum(energy):\n",
    "            ans = sum(energy) - initialEnergy + 1\n",
    "        print(ans)\n",
    "        n = len(experience)\n",
    "        for i in range(n):\n",
    "            if initialExperience > experience[i]:\n",
    "                initialExperience += experience[i]\n",
    "            else:\n",
    "                ans += (experience[i] - initialExperience + 1)\n",
    "                initialExperience = initialExperience + experience[i] + (experience[i] - initialExperience + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        n = len(energy)\n",
    "        \n",
    "        a, b = initialEnergy, initialExperience\n",
    "        da, db = 0, 0\n",
    "        for i in range(n):\n",
    "            if a <= energy[i]:\n",
    "                da += energy[i] - a + 1\n",
    "                a = energy[i] + 1\n",
    "            if b <= experience[i]:\n",
    "                db += experience[i] - b + 1\n",
    "                b = experience[i] + 1\n",
    "            a -= energy[i]\n",
    "            b += experience[i]\n",
    "\n",
    "        return da + db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = max(sum(energy) + 1 - initialEnergy, 0)\n",
    "        required_exp = 0\n",
    "        exp = initialExperience\n",
    "        for i in experience:\n",
    "            if exp <= i:\n",
    "                required_exp = max(required_exp, i - exp + 1)\n",
    "            exp += i\n",
    "        ans += required_exp\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sum_ene,time_ene,time_exp = 0,0,0\n",
    "        sum_ene = sum(energy)\n",
    "        print(sum_ene)\n",
    "        if len(energy) < 2:\n",
    "            if initialEnergy > energy[0] and initialExperience > experience[0]:\n",
    "                return 0\n",
    "            \n",
    "        if sum_ene >= initialEnergy:\n",
    "            time_ene = sum_ene - initialEnergy + 1\n",
    "        \n",
    "        i,all_time = 0,0\n",
    "        sum_exp = initialExperience\n",
    "        #print(len(experience))\n",
    "        while i < len(experience):\n",
    "            if experience[i] >= sum_exp:\n",
    "                time_exp = experience[i] - sum_exp + 1\n",
    "                all_time += time_exp\n",
    "                sum_exp += time_exp + experience[i]\n",
    "                i += 1\n",
    "            else:\n",
    "                sum_exp += experience[i]\n",
    "                i += 1\n",
    "        print(time_ene,time_exp,sum_exp-initialExperience,sum(experience))\n",
    "        return (time_ene + all_time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = 0\n",
    "        required_energy = sum(energy) + 1\n",
    "        if initialEnergy < required_energy:\n",
    "            ans += required_energy - initialEnergy\n",
    "        required_exp = 0\n",
    "        exp = initialExperience\n",
    "        for i in experience:\n",
    "            if exp <= i:\n",
    "                required_exp = max(required_exp, i - exp + 1)\n",
    "            exp += i\n",
    "        ans += required_exp\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, eng: int, exp: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x, y in zip(energy, experience):\n",
    "            if eng <= x:\n",
    "                ans += x + 1 - eng\n",
    "                eng = x + 1 # 补到刚好超过\n",
    "            eng -= x\n",
    "            if exp <= y:\n",
    "                ans += y + 1 - exp\n",
    "                exp = y + 1 # 补到刚好超过\n",
    "            exp += y\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        trainingHours = 0 if initialEnergy > sm else sm + 1 - initialEnergy\n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                trainingHours += 1 + (e - initialExperience)\n",
    "                initialExperience = 2 * e + 1\n",
    "            else:\n",
    "                initialExperience += e\n",
    "        return trainingHours\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ret = max(0,sum(energy)-initialEnergy+1)\n",
    "        max_val = 0\n",
    "        for i in range(len(experience)):\n",
    "            max_val = max(max_val,experience[i]-initialExperience+1)\n",
    "            initialExperience += experience[i]\n",
    "        return ret+max_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if initialEnergy < sum(energy) + 1:\n",
    "            ans += sum(energy) + 1 - initialEnergy\n",
    "        for i in experience:\n",
    "            if initialExperience <= i:\n",
    "                ans += i + 1 - initialExperience\n",
    "                initialExperience = i + 1\n",
    "            initialExperience += i\n",
    "            if initialExperience > max(experience):\n",
    "                break\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ienergy,iexperience=initialEnergy,initialExperience        \n",
    "        itraining=max(0,sum(energy)-ienergy+1) \n",
    "        for x in experience:                        \n",
    "            if iexperience <= x:\n",
    "                itraining+=x-iexperience+1\n",
    "                iexperience = x+1            \n",
    "            iexperience+=x\n",
    "            #print('-',itraining,ienergy,iexperience,energy,experience)\n",
    "        return itraining"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        e = 0\n",
    "        for i in energy:\n",
    "            e += i\n",
    "        sums = 0\n",
    "        if initialEnergy - e <= 0:\n",
    "            sums += e - initialEnergy +1\n",
    "        for i in experience:\n",
    "            if initialExperience > i:\n",
    "                initialExperience = initialExperience + i\n",
    "            else:\n",
    "                sums += i - initialExperience + 1\n",
    "                initialExperience = i*2+1\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sumEnergy = sum(energy)\n",
    "        trainingHours = 0 if initialEnergy > sumEnergy else sumEnergy + 1 - initialEnergy\n",
    "        for exp in experience:\n",
    "            if initialExperience <= exp:\n",
    "                trainingHours += 1 + (exp - initialExperience)\n",
    "                initialExperience = 2 * exp + 1\n",
    "            else:\n",
    "                initialExperience += exp\n",
    "        return trainingHours\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x,y in zip(energy, experience):\n",
    "            if initialEnergy <= x:\n",
    "                ans += x + 1 - initialEnergy\n",
    "                initialEnergy = x + 1\n",
    "            initialEnergy -= x\n",
    "            if initialExperience <= y:\n",
    "                ans += y + 1 - initialExperience\n",
    "                initialExperience = y + 1\n",
    "            initialExperience += y\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        e1=[]\n",
    "        e2=[]\n",
    "        for i in range(len(energy)):\n",
    "            e1.append(initialEnergy-energy[i])\n",
    "            initialEnergy-=energy[i]\n",
    "            e2.append(initialExperience-experience[i])\n",
    "            initialExperience+=experience[i]\n",
    "        count=0\n",
    "        if min(e1)<=0:\n",
    "            count+=(abs(min(e1))+1)\n",
    "        if min(e2)<=0:\n",
    "            count+=(abs(min(e2))+1)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        e = sum(energy)\n",
    "        if initialEnergy > e:\n",
    "            ans = 0\n",
    "        else:\n",
    "            ans = e - initialEnergy + 1\n",
    "        # 经验判断\n",
    "        for exp in experience:\n",
    "            if initialExperience <= exp:\n",
    "                change = exp - initialExperience + 1\n",
    "                ans += change\n",
    "                initialExperience += change\n",
    "            initialExperience += exp\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        l,ans = len(energy),0\n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                t = e - initialExperience + 1\n",
    "                initialExperience += t + e\n",
    "                ans += t\n",
    "            else:\n",
    "                initialExperience += e\n",
    "        initialEnergy -= sum(energy)\n",
    "        if initialEnergy > 0:\n",
    "            return ans\n",
    "        return abs(initialEnergy) + 1 + ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        trainingHours = 0 if initialEnergy > sm else sm + 1 - initialEnergy\n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                trainingHours += 1 + (e - initialExperience)\n",
    "                initialExperience = 2 * e + 1\n",
    "            else:\n",
    "                initialExperience += e\n",
    "        return trainingHours\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ret = 0\n",
    "        en, ex = initialEnergy, initialExperience\n",
    "        for i in range(len(energy)):\n",
    "            if en <= energy[i]:\n",
    "                ret += energy[i] - en + 1\n",
    "                en = energy[i] + 1\n",
    "            if ex <= experience[i]:\n",
    "                ret += experience[i] + 1 - ex\n",
    "                ex = experience[i] + 1\n",
    "            en -= energy[i]\n",
    "            ex += experience[i]\n",
    "        return ret\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if initialEnergy < sum(energy) + 1:\n",
    "            ans += sum(energy) + 1 - initialEnergy\n",
    "        for i in experience:\n",
    "            if initialExperience <= i:\n",
    "                ans += i + 1 - initialExperience\n",
    "                initialExperience = i + 1\n",
    "            initialExperience += i\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = max(sum(energy) + 1 - initialEnergy, 0)\n",
    "        experience = [initialExperience] + experience\n",
    "        for i in range(1, len(experience)):\n",
    "            a = 0\n",
    "            if experience[i] >= experience[i - 1]:\n",
    "                a = experience[i] - experience[i - 1] + 1\n",
    "                ans += a\n",
    "            experience[i] += experience[i - 1] + a\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        if initialEnergy > sm:\n",
    "            trainingHours = 0  \n",
    "        else:\n",
    "            trainingHours = sm + 1 - initialEnergy\n",
    "        \n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                trainingHours += 1 + (e - initialExperience)\n",
    "                initialExperience = 2 * e + 1\n",
    "            else:\n",
    "                initialExperience += e\n",
    "        return trainingHours\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        nums = 0 if initialEnergy > sm else sm + 1 -initialEnergy\n",
    "        for i in experience:\n",
    "            if initialExperience <= i:\n",
    "                nums += i - initialExperience +1\n",
    "                initialExperience = 2 * i +1\n",
    "            else:\n",
    "                initialExperience += i\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = max(sum(energy) + 1 - initialEnergy, 0)\n",
    "        for i in experience:\n",
    "            if initialExperience > max(experience):\n",
    "                break\n",
    "            if initialExperience <= i:\n",
    "                ans += i + 1 - initialExperience\n",
    "                initialExperience = i + 1\n",
    "            initialExperience += i\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        n = len(energy)\n",
    "        train = 0\n",
    "        for i in range(n):\n",
    "            sub_energy = initialEnergy - energy[i]\n",
    "            sub_experience = initialExperience - experience[i]\n",
    "            if sub_energy > 0 and sub_experience > 0 :\n",
    "                initialEnergy -= energy[i]\n",
    "                initialExperience += experience[i]\n",
    "            elif sub_energy <= 0 and sub_experience <= 0:\n",
    "                train -= sub_energy\n",
    "                train -= sub_experience\n",
    "                train += 2\n",
    "\n",
    "                initialEnergy -= sub_energy\n",
    "                initialEnergy += 1\n",
    "                initialEnergy -= energy[i]\n",
    "\n",
    "                initialExperience -= sub_experience\n",
    "                initialExperience += 1\n",
    "                initialExperience += experience[i]\n",
    "            elif sub_energy <= 0:\n",
    "                train -= sub_energy\n",
    "                train += 1\n",
    "\n",
    "                initialEnergy -= sub_energy\n",
    "                initialEnergy += 1\n",
    "                initialEnergy -= energy[i]\n",
    "\n",
    "                initialExperience += experience[i]\n",
    "            else:\n",
    "                train -= sub_experience\n",
    "                train += 1\n",
    "\n",
    "                initialEnergy -= energy[i]\n",
    "\n",
    "                initialExperience -= sub_experience\n",
    "                initialExperience += 1\n",
    "                initialExperience += experience[i]\n",
    "        return train\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if initialEnergy < sum(energy) + 1:\n",
    "            ans += sum(energy) + 1 - initialEnergy\n",
    "        for i in experience:\n",
    "            if initialExperience > max(experience):\n",
    "                break\n",
    "            if initialExperience <= i:\n",
    "                ans += i + 1 - initialExperience\n",
    "                initialExperience = i + 1\n",
    "            initialExperience += i\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        n = len(energy)\n",
    "        a = max(0, sum(energy) + 1 - initialEnergy, 0)\n",
    "        for b in range(1000):\n",
    "            e2 = initialExperience + b\n",
    "            for i in range(n):\n",
    "                ex = experience[i]   \n",
    "                if ex >= e2:\n",
    "                    break\n",
    "                e2 += ex\n",
    "            else:\n",
    "                return a + b\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        add_energy=0\n",
    "        add_experience=0\n",
    "        for i in range(len(energy)):\n",
    "            if initialExperience>experience[i]:\n",
    "                initialExperience+=experience[i]\n",
    "                if initialEnergy>energy[i]:\n",
    "                    initialEnergy-=energy[i]\n",
    "                else:\n",
    "                    add_energy+=(energy[i]+1-initialEnergy)\n",
    "                    initialEnergy=1\n",
    "            else:\n",
    "                add_experience+=(experience[i]+1-initialExperience)\n",
    "                initialExperience=2*experience[i]+1\n",
    "                if initialEnergy>energy[i]:\n",
    "                    initialEnergy-=energy[i]\n",
    "                else:\n",
    "                    add_energy+=(energy[i]+1-initialEnergy)\n",
    "                    initialEnergy=1\n",
    "        return add_experience+add_energy\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        time = 0\n",
    "        for x,y in zip(energy,experience):\n",
    "            if initialExperience <= y:\n",
    "                time += y-initialExperience+1\n",
    "                initialExperience = y+1\n",
    "            if initialEnergy <= x:\n",
    "                time += x+1-initialEnergy\n",
    "                initialEnergy = x+1\n",
    "            initialEnergy -= x\n",
    "            initialExperience += y\n",
    "        return time\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        curEn, curEx = initialEnergy, initialExperience\n",
    "        res = 0\n",
    "        for en, ex in zip(energy, experience):\n",
    "            if curEn <= en:\n",
    "                res += en - curEn + 1\n",
    "                curEn = 1\n",
    "            else:\n",
    "                curEn -= en\n",
    "            if curEx <= ex:\n",
    "                res += ex - curEx + 1\n",
    "                curEx = ex*2 + 1\n",
    "            else:\n",
    "                curEx += ex\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        res = 0\n",
    "        cur_energy = initialEnergy\n",
    "        cur_exp = initialExperience\n",
    "        n = len(energy)\n",
    "        for i in range(n):\n",
    "            if cur_energy <= energy[i]:\n",
    "                res += energy[i] - cur_energy + 1\n",
    "                cur_energy = energy[i] + 1\n",
    "            cur_energy = cur_energy - energy[i]\n",
    "            if cur_exp <= experience[i]:\n",
    "                res += experience[i] - cur_exp + 1\n",
    "                cur_exp = experience[i] + 1\n",
    "            cur_exp = cur_exp + experience[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = max(sum(energy) + 1 - initialEnergy, 0)\n",
    "        experience = [initialExperience] + experience\n",
    "        for i in range(1, len(experience)):\n",
    "            a = 0\n",
    "            if experience[i] >= experience[i - 1]:\n",
    "                a = experience[i] - experience[i - 1] + 1\n",
    "                ans += a\n",
    "            experience[i] += experience[i - 1] + a\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        x,y=0,0\n",
    "        l=len(energy)\n",
    "        for k in range(l):\n",
    "            if initialEnergy<=energy[k]:\n",
    "                x=max(x,energy[k]-initialEnergy+1)\n",
    "            if initialExperience<=experience[k]:\n",
    "                y=max(y,experience[k]-initialExperience+1)\n",
    "            initialEnergy-=energy[k]\n",
    "            initialExperience+=experience[k]\n",
    "        return x+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution: minNumberOfHours=lambda self, initialEnergy, initialExperience, energy, experience: max(initialExperience, max(exp - prefix + 1 for exp, prefix in zip(experience, accumulate(experience, initial=0)))) - initialExperience + max(sum(energy) - initialEnergy + 1, 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    '''\n",
    "    1。如果初始精力值大于所有对手的精力值之和，那么不需要训练。\n",
    "    2、因为每轮次都需要消耗精力，所以需要的精力是可以在比赛前就算出来的，直接对精力进行计算。\n",
    "    而经验是动态变化的，思路就是，高于对手就不训练，反正打完会加，如果比对手低，再根据差值进行训练\n",
    "\n",
    "\n",
    "    '''\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int],\n",
    "                         experience: List[int]) -> int:\n",
    "        # 根据精力值计算精力需要的时间。\n",
    "        if initialEnergy>sum(energy):\n",
    "            ans=0\n",
    "        else:\n",
    "            ans=sum(energy)+1-initialEnergy\n",
    "        # 设置变量，energy 可以直接弄一个最终值，它就是初始值+训练时长（获取精力）\n",
    "        exp=initialExperience\n",
    "        #\n",
    "        en=initialEnergy+ans\n",
    "        # 比赛轮次,每轮次开始前\n",
    "        n=len(energy)\n",
    "        for i in range(n):\n",
    "            # 因为在比赛开始就有需要的精力，在每轮次里就只需要判断经验。当前状态的经验值>对手的经验值，就不需要处理。\n",
    "            # 如果当前状态的经验值<对手经验值，则需要进行训练获取经验值。\n",
    "            # 当前经验值低于对手经验值，由于需要绝对高于对手经验值，\n",
    "            if exp<=experience[i]:\n",
    "                # 需要进行的训练时长\n",
    "                # 绝对大于对手的经验，你3 我必须 4 起步\n",
    "                time=experience[i]+1-exp\n",
    "                # 把本次需要的训练时长，加到总的训练时长里\n",
    "                ans+=time\n",
    "                # 训练结束后，经验值达到比对手+1，此时的经验值是：\n",
    "                exp=experience[i]+1\n",
    "            # 比赛结束后，exp 加上击败对手的经验值    \n",
    "            exp+=experience[i]\n",
    "            en-=energy[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm=sum(energy)\n",
    "        trainhours=0 if initialEnergy>sm else sm+1-initialEnergy\n",
    "        for i in experience:\n",
    "            if initialExperience<=i:\n",
    "                trainhours+=1+(i-initialExperience)\n",
    "                initialExperience=i*2+1\n",
    "            else:\n",
    "                initialExperience+=i\n",
    "        return trainhours"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        # 真繁琐\n",
    "        # 因为经历在对战时只会减少，不会增加，所以一开始精力就要大于所有对手的精力之和\n",
    "        ans = 0\n",
    "        ans += max(sum(energy) - initialEnergy + 1, 0)\n",
    "        # 然后是经验，经验在对战时，只会增加不会减少，但是，必须要大于才能战胜对手\n",
    "        for i in range(101):\n",
    "            exp = initialExperience + i\n",
    "            flag = True\n",
    "            for e in experience:\n",
    "                if e >= exp:\n",
    "                    flag = False\n",
    "                    break\n",
    "                else: exp += e\n",
    "            if flag:\n",
    "                break\n",
    "        \n",
    "        \n",
    "        return ans + i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ex_energy = max(0,sum(energy) + 1 - initialEnergy)\n",
    "        mine = 1\n",
    "        for i in range(1,max(experience)+2):\n",
    "            flag = 1\n",
    "            init =  i\n",
    "            for e in experience:\n",
    "                if e >= init:\n",
    "                    flag = 0\n",
    "                    break\n",
    "                else:\n",
    "                    init += e\n",
    "            if flag == 1:\n",
    "                mine = i\n",
    "                break\n",
    "        return ex_energy+max(mine-initialExperience,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        n = len(energy)\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            if initialEnergy <= energy[i]:\n",
    "                ans += energy[i] - initialEnergy + 1\n",
    "                initialEnergy += energy[i] - initialEnergy + 1\n",
    "            if initialExperience <= experience[i]:\n",
    "                ans += experience[i] - initialExperience + 1\n",
    "                initialExperience +=experience[i] - initialExperience + 1\n",
    "            initialEnergy -= energy[i]\n",
    "            initialExperience += experience[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        trainEnergy = 0\n",
    "        if initialEnergy<=sum(energy):\n",
    "            trainEnergy = sum(energy)+1-initialEnergy\n",
    "        trainexperience = 0\n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                trainexperience = e - initialExperience +1 +trainexperience\n",
    "                initialExperience = e+1\n",
    "            initialExperience = initialExperience+e\n",
    "        return trainEnergy+trainexperience\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans = max(0, sum(energy) + 1 - initialEnergy)\n",
    "        for exp in experience:\n",
    "            if initialExperience <= exp:\n",
    "                ans += (exp + 1 - initialExperience)\n",
    "                initialExperience = exp + 1\n",
    "            initialExperience += exp\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    '''\n",
    "    1。如果初始精力值大于所有对手的精力值之和，那么不需要训练。\n",
    "    2、因为每轮次都需要消耗精力，所以需要的精力是可以在比赛前就算出来的，直接对精力进行计算。\n",
    "    而经验是动态变化的，思路就是，高于对手就不训练，反正打完会加，如果比对手低，再根据差值进行训练\n",
    "\n",
    "\n",
    "    '''\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int],\n",
    "                         experience: List[int]) -> int:\n",
    "        # 根据精力值计算精力需要的时间。\n",
    "        if initialEnergy>sum(energy):\n",
    "            ans=0\n",
    "        else:\n",
    "            ans=sum(energy)+1-initialEnergy\n",
    "        # 设置变量，energy 可以直接弄一个最终值，它就是初始值+训练时长（获取精力）\n",
    "        exp=initialExperience\n",
    "        #\n",
    "        en=initialEnergy+ans\n",
    "        # 比赛轮次,每轮次开始前\n",
    "        n=len(energy)\n",
    "        for i in range(n):\n",
    "            # 因为在比赛开始就有需要的精力，在每轮次里就只需要判断经验。当前状态的经验值>对手的经验值，就不需要处理。\n",
    "            # 如果当前状态的经验值<对手经验值，则需要进行训练获取经验值。\n",
    "            # 当前经验值低于对手经验值，由于需要绝对高于对手经验值，\n",
    "            if exp<=experience[i]:\n",
    "                # 需要进行的训练时长\n",
    "                # 绝对大于对手的经验，你3 我必须 4 起步\n",
    "                time=experience[i]+1-exp\n",
    "                # 把本次需要的训练时长，加到总的训练时长里\n",
    "                ans+=time\n",
    "                # 训练结束后，经验值达到比对手+1，此时的经验值是：\n",
    "                exp=experience[i]+1\n",
    "            # 比赛结束后，exp 加上击败对手的经验值    \n",
    "            exp+=experience[i]\n",
    "            en-=energy[i]\n",
    "        return ans\n",
    "\n",
    "# class Solution:\n",
    "#     '''\n",
    "#     1。如果初始精力值大于所有对手的精力值之和，那么不需要训练。\n",
    "#     2、因为每轮次都需要消耗精力，所以需要的精力是可以在比赛前就算出来的，直接对精力进行计算。\n",
    "#     而经验是动态变化的，思路就是，高于对手就不训练，反正打完会加，如果比对手低，再根据差值进行训练\n",
    "    \n",
    "    \n",
    "#     '''\n",
    "#     def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int],\n",
    "#                          experience: List[int]) -> int:\n",
    "#         # ans，需要训练的时间，一小时 1 点，可以加精力也可以加经验\n",
    "#         if initialEnergy > sum(energy):\n",
    "#             ans = 0\n",
    "#         else:\n",
    "#             ans = sum(energy) + 1 - initialEnergy\n",
    "#         exp = initialExperience\n",
    "#         en = initialEnergy\n",
    "#         n = len(energy)\n",
    "#         # 没轮次比赛开始前，精力和经验值是变化的\n",
    "#         for i in range(n):\n",
    "#             if exp <= experience[i]:\n",
    "#                 # 绝对大于对手的经验，你3 我必须 4 起步\n",
    "#                 time = experience[i] + 1 - exp\n",
    "#                 ans+=time\n",
    "#                 # 经过训练增加经验值之后，达到比对手+1，此时的经验值是：\n",
    "#                 exp=experience[i]+1\n",
    "#             #比赛结束后，exp 加上击败对手的经验值     \n",
    "#             exp+=experience[i]\n",
    "#             en=en-energy[i]\n",
    "#         return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        ans1 = 0\n",
    "        for i in experience:\n",
    "            if initialExperience <= i:\n",
    "                ans1 += i - initialExperience + 1\n",
    "                initialExperience = i + 1\n",
    "            initialExperience += i\n",
    "        ans2 = max(0, sum(energy) - initialEnergy + 1)  \n",
    "        return ans1 + ans2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        trainingHours = 0 if initialEnergy>sm else sm+1-initialEnergy\n",
    "        for e in experience:\n",
    "            if initialExperience<=e:\n",
    "                trainingHours += 1+ (e-initialExperience)\n",
    "                initialExperience = 2*e+1\n",
    "            else:\n",
    "                initialExperience += e\n",
    "        return trainingHours"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        if sum(experience) < initialExperience and sum(energy) < initialEnergy:\n",
    "            return 0\n",
    "        cnt = 0\n",
    "        # ex每次对战后剩余的经验，要保证必须是最小为1\n",
    "        current = initialExperience\n",
    "        for i in range(len(energy)):\n",
    "            if current > experience[i]:\n",
    "                #当前的持有经验大于对手的经验，记录下当前剩余的经验\n",
    "                current += experience[i]\n",
    "            elif current == experience[i]:\n",
    "                #当前的经验恰好等于对手的经验，那么实际上初始经验应当增加1\n",
    "                current = experience[i] * 2 + 1\n",
    "                cnt += 1\n",
    "            else:\n",
    "                #当前的经验小于对手的经验，那么实际上应当增加差值加1\n",
    "                cnt += experience[i] - current + 1\n",
    "                current = experience[i] * 2 + 1\n",
    "\n",
    "\n",
    "        if sum(energy) >= initialEnergy:\n",
    "            return sum(energy) - initialEnergy + cnt + 1 \n",
    "        else:\n",
    "            return cnt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        ans = 0 if initialEnergy > sm else sm + 1 - initialEnergy\n",
    "        # ans = max(sum(energy) + 1 - initialEnergy, 0)\n",
    "        # for e in experience[::-1]:\n",
    "        #     if exp <= e:\n",
    "        #         exp = e + 1\n",
    "        #     else:\n",
    "        #         exp -= e\n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                ans += (e - initialExperience + 1)\n",
    "                initialExperience = 2 * e + 1\n",
    "            else:\n",
    "                initialExperience += e\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sum_energy=sum(energy)\n",
    "        need_time=sum_energy-initialEnergy+1 if sum_energy>=initialEnergy else 0\n",
    "        for i in experience:\n",
    "            if initialExperience<=i:\n",
    "                time=i-initialExperience+1\n",
    "                initialExperience+=time\n",
    "                need_time+=time\n",
    "            initialExperience+=i\n",
    "        return need_time\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        e = sum(energy)\n",
    "        sums = 0\n",
    "        if initialEnergy - e <= 0:\n",
    "            sums += e - initialEnergy +1\n",
    "        for i in experience:\n",
    "            if initialExperience > i:\n",
    "                initialExperience = initialExperience + i\n",
    "            else:\n",
    "                sums += i - initialExperience + 1\n",
    "                initialExperience = i*2+1\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sen, sex = sum(energy), sum(experience)\n",
    "        if sex < initialExperience and sen < initialEnergy:\n",
    "            return 0\n",
    "        cnt = 0\n",
    "        # ex每次对战后剩余的经验，要保证必须是最小为1\n",
    "        current = initialExperience\n",
    "        n = len(energy)\n",
    "        for i in range(n):\n",
    "            if current > experience[i]:\n",
    "                #当前的持有经验大于对手的经验，记录下当前剩余的经验\n",
    "                current += experience[i]\n",
    "            else:\n",
    "                #当前的经验恰好等于对手的经验，那么实际上初始经验应当增加1\n",
    "                #当前的经验小于对手的经验，那么实际上应当增加差值加1\n",
    "                cnt += experience[i] - current + 1\n",
    "                current = experience[i] * 2 + 1\n",
    "\n",
    "\n",
    "        if sum(energy) >= initialEnergy:\n",
    "            return sum(energy) - initialEnergy + cnt + 1 \n",
    "        else:\n",
    "            return cnt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        need=[]\n",
    "        for i in range(len(experience)):\n",
    "            if i == 0:\n",
    "                need.append(experience[i]+1)\n",
    "            else:\n",
    "                need.append(experience[i]+1-sum(experience[0:i]))\n",
    "        return max(sum(energy)+1-initialEnergy,0)+max(max(need)-initialExperience,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sum_ene,time_ene,time_exp = 0,0,0\n",
    "        sum_ene = sum(energy)        \n",
    "            \n",
    "        if sum_ene >= initialEnergy:\n",
    "            time_ene = sum_ene - initialEnergy + 1\n",
    "        \n",
    "        i,all_time = 0,0\n",
    "        sum_exp = initialExperience\n",
    "        #print(len(experience))\n",
    "        while i < len(experience):\n",
    "            if experience[i] >= sum_exp:\n",
    "                time_exp = experience[i] - sum_exp + 1\n",
    "                all_time += time_exp\n",
    "                sum_exp += time_exp + experience[i]\n",
    "                i += 1\n",
    "            else:\n",
    "                sum_exp += experience[i]\n",
    "                i += 1\n",
    "        print(time_ene,time_exp,sum_exp-initialExperience,sum(experience))\n",
    "        return (time_ene + all_time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        necessary_energy = sum(energy) + 1\n",
    "        necessary_experience = initialExperience\n",
    "\n",
    "        extra_energy = max(0, necessary_energy - initialEnergy) \n",
    "        extra_experience = 0\n",
    "\n",
    "        for i in range(len(experience)):\n",
    "            if necessary_experience < experience[i] + 1:\n",
    "                extra_experience += experience[i] + 1 - necessary_experience\n",
    "                necessary_experience = experience[i] + 1\n",
    "            necessary_experience += experience[i]\n",
    "\n",
    "        return extra_energy + extra_experience\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        r = 0\n",
    "        if sum(energy) >= initialEnergy:\n",
    "            r = sum(energy) - initialEnergy+1\n",
    "\n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                r += (e - initialExperience + 1)\n",
    "                initialExperience = e+1\n",
    "            \n",
    "            initialExperience += e\n",
    "        \n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        trainingHours = 0 if initialEnergy > sm else sm + 1 - initialEnergy\n",
    "\n",
    "        for e in experience:\n",
    "            if initialExperience <= e:\n",
    "                trainingHours += (e - initialExperience) + 1\n",
    "                initialExperience = 2 * e + 1\n",
    "\n",
    "            else:\n",
    "                initialExperience += e\n",
    "        \n",
    "        return trainingHours"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        # 最少的精力\n",
    "        en_time = sum(energy) - initialEnergy + 1\n",
    "        # 最少的经验 \n",
    "        li = []\n",
    "        if len(energy) == 1:\n",
    "            li.append(experience[0]-initialExperience + 1)\n",
    "        else:\n",
    "            for i in range(len(experience)-1):\n",
    "                li.append(experience[i+1] - sum(experience[:i+1]) - initialExperience + 1)\n",
    "            li.append(experience[0]-initialExperience + 1)\n",
    "        if en_time <= 0 and max(li) <= 0:\n",
    "            return 0\n",
    "        if en_time <= 0 and max(li) > 0:\n",
    "            return max(li)\n",
    "        if en_time > 0 and max(li) <= 0:\n",
    "            return en_time\n",
    "        if en_time > 0 and max(li) > 0:\n",
    "            return en_time + max(li)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        a=0\n",
    "        b=0\n",
    "        for i in range(len(energy)):\n",
    "            if initialEnergy<=energy[i]:\n",
    "                x=energy[i]-initialEnergy+1\n",
    "                a+=x\n",
    "                initialEnergy=initialEnergy+x-energy[i]\n",
    "            elif initialEnergy>energy[i]:\n",
    "                initialEnergy-=energy[i]\n",
    "            if initialExperience<=experience[i]:\n",
    "                y=experience[i]-initialExperience+1\n",
    "                b+=y\n",
    "                initialExperience=initialExperience+y+experience[i]\n",
    "            elif initialExperience>experience[i]:\n",
    "                initialExperience+=experience[i]\n",
    "        return a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        res = 0\n",
    "        eng = initialEnergy\n",
    "        exp = initialExperience\n",
    "        for x,y in zip(energy,experience):\n",
    "            if eng <= x:\n",
    "                res += x - eng + 1\n",
    "                eng = x + 1\n",
    "            eng -= x\n",
    "            if exp <= y:\n",
    "                res += y - exp + 1\n",
    "                exp = y + 1\n",
    "            exp += y\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        n = len(energy)\n",
    "\n",
    "        delta_energy, delta_experience = 0, 0\n",
    "        if n == 1:\n",
    "            if initialEnergy <= energy[0]:\n",
    "                delta_energy = energy[0] - initialEnergy + 1\n",
    "            if initialExperience <= experience[0]:\n",
    "                delta_experience = experience[0] - initialExperience + 1\n",
    "            return delta_energy + delta_experience\n",
    "        \n",
    "        sum_energy, sum_experience = 0, 0\n",
    "        for i in range(n):\n",
    "            sum_energy += energy[i]\n",
    "            delta_energy = max(delta_energy, sum_energy - initialEnergy + 1)\n",
    "            delta_experience = max(delta_experience, experience[i] - sum_experience - initialExperience + 1)\n",
    "            sum_experience += experience[i]\n",
    "        return delta_energy + delta_experience"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        sm = sum(energy)\n",
    "        if initialEnergy>sm:\n",
    "            trainhoue = 0\n",
    "        else:\n",
    "            trainhoue = sm+1-initialEnergy\n",
    "        for e in experience:\n",
    "            if initialExperience<=e:\n",
    "                trainhoue+=1+(e-initialExperience)\n",
    "                initialExperience = 2*e+1\n",
    "            else:\n",
    "                initialExperience+=e\n",
    "        return trainhoue\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        t = 0\n",
    "        cur_exp = initialExperience\n",
    "        cur_eng = initialEnergy\n",
    "        for i in range(len(experience)):\n",
    "            if experience[i] >= cur_exp:\n",
    "                d = experience[i] - cur_exp + 1\n",
    "                t += d\n",
    "                cur_exp += d\n",
    "            cur_exp += experience[i]\n",
    "            if energy[i] >= cur_eng:\n",
    "                d = energy[i] - cur_eng + 1\n",
    "                t += d\n",
    "                cur_eng += d\n",
    "            cur_eng -= energy[i]\n",
    "\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        se = sum(energy)\n",
    "        t = 0\n",
    "        if initialEnergy > se:\n",
    "            t = 0 \n",
    "        else:\n",
    "            t += se + 1 - initialEnergy \n",
    "        e = initialExperience \n",
    "        for x in experience:\n",
    "            if e > x:\n",
    "                e += x \n",
    "            else:\n",
    "                t += x + 1 - e \n",
    "                e = 2 * x + 1\n",
    "        return t \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int:\n",
    "        e,x=0,0\n",
    "        y=0\n",
    "        for i in experience:\n",
    "            x=max(x,i-sum(experience[0:y])-initialExperience+1)\n",
    "            print(x)\n",
    "            y+=1\n",
    "        e+=max(0,sum(energy)-initialEnergy+1)\n",
    "        print(e,x)\n",
    "        return e+x"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
